package com.hn.gege.sys.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.sys.modules.sys.dto.OrganizationPageDTO;
import com.hn.gege.sys.modules.sys.entity.Organization;
import com.hn.gege.sys.modules.sys.service.IOrganizationService;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.common.BaseController;
import com.hn.gege.core.query.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "SysOrganizationController", description = "机构管理")
@RestController
@RequestMapping("/sys/organization")
@Validated
public class OrganizationController extends BaseController {

    @Autowired
    IOrganizationService organizationService;

    @ApiOperation("分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<OrganizationPageDTO> pageInfo(@NotNull Integer pageNo,
                                                    @NotNull Integer pageSize,
                                                    Organization organization,
                                                    HttpServletRequest request){
        Page<Organization> pageInfo = new Page<>(pageNo, pageSize);
        //构造查询条件
        QueryWrapper<Organization> qw = QueryGenerator.initQueryWrapper(organization, request.getParameterMap());
        Page<OrganizationPageDTO> page1 = organizationService.myPage(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("机构列表")
    @GetMapping
    public CommonResult<List<Organization>> list(){
        List<Organization> list = organizationService.list();
        return CommonResult.success(list);
    }

    @ApiOperation("通过主键获取")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "机构代码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{code}")
    public CommonResult<Organization> getByPk(@NotBlank @PathVariable("code") String code){
        Organization organization = organizationService.getByPk(code);
        return CommonResult.success(organization);
    }

    @ApiOperation("新增")
    @PostMapping
    public CommonResult add(@Validated Organization organization, BindingResult bindingResult){
    boolean b = organizationService.save(organization);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改")
    @PutMapping
    public CommonResult updateByPk(@Validated Organization organization, BindingResult bindingResult){
        boolean b = organizationService.updateByPk(organization);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "机构代码", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{code}")
    public CommonResult removeByPk(@NotBlank @PathVariable("code") String code){
        boolean b = organizationService.removeByPk(code);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
